package terrain.impl.first;
import java.awt.Color;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import factory.Constants;

import terrain.impl.first.Point;





public class Box {

	
	private int width;
	private int height;
	private boolean isFree;
	private int x;
	private int y;
	private Point points[];
	int percentageX;
	int percentageY;
	
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public boolean isFree() {
		return isFree;
	}

	public void setFree(boolean isFree) {
		this.isFree = isFree;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getXMid(){
		return x+Constants.BOX_WIDTH/2;
	}
	public int getYMid(){
		return y+Constants.BOX_HEIGHT/2;
	}
	
	public Box(int x, int y){
		isFree=true;
		width = Constants.BOX_WIDTH;
		height = Constants.BOX_HEIGHT;
		this.x=x*Constants.BOX_WIDTH;
		this.y=y*Constants.BOX_HEIGHT;
		points = new Point [5];
		//25%
		percentageX = (int) (Constants.BOX_WIDTH*0.25); 
		percentageY = (int) (Constants.BOX_WIDTH*0.25);
				
		
		points[0] = new Point(percentageX,percentageY);
		points[1] = new Point(Constants.BOX_WIDTH-percentageX,percentageY);
		points[2] = new Point(percentageX*2,percentageY*2);
		points[3] = new Point(percentageX,Constants.BOX_HEIGHT-percentageY);
		points[4] = new Point(Constants.BOX_WIDTH-percentageX,Constants.BOX_HEIGHT-percentageY);
		
	}

	public void paint(Graphics g) {
		
		if(isFree){
			g.setColor(Color.GREEN);
		}
		else{
			g.setColor(Color.RED);
		}
		//paint the box
		//g.drawRect(x+10, y+10, Constants.BOX_WIDTH, Constants.BOX_HEIGHT);
		g.fillRect(x, y, Constants.BOX_WIDTH, Constants.BOX_HEIGHT);
		g.setColor(Color.BLACK);
		//horizontal lines
		g.drawLine(x,y,x+Constants.BOX_WIDTH,y);
		g.drawLine(x,y+Constants.BOX_HEIGHT,x+Constants.BOX_WIDTH,y+Constants.BOX_HEIGHT);
		//vertical lines
		g.drawLine(x+Constants.BOX_WIDTH,y,x+Constants.BOX_WIDTH,y+Constants.BOX_HEIGHT);
		g.drawLine(x,y,x,y+Constants.BOX_HEIGHT);
		
		for(int i=0;i<5;i++){
			points[i].paint(x,y,g);
		}
		
	}

	public void calculateIntersection(BufferedImage image, int freeColor) {
		int contFree=0;
		for(int i=0;i<5;i++){
			points[i].calculateIntersection(image,freeColor,x,y);
			if(!points[i].isFree()){
				contFree++;
			}
		}
		if(contFree>=2){
			isFree=false;
		}
	}

	public String save(String save) {
		save=save +Constants.BOX_WORLD_ID+","+isFree;
		return save;
	}

	public Point getPoint(int k) {
		percentageX = (int) (Constants.BOX_WIDTH*0.25); 
		percentageY = (int) (Constants.BOX_WIDTH*0.25);
		switch(k){
		case 0:
			return new Point(percentageX,percentageY);
		case 1:
			return new Point(Constants.BOX_WIDTH-percentageX,percentageY);
		case 2:
			return new Point(percentageX*2,percentageY*2);
		case 3:
			return new Point(percentageX,Constants.BOX_HEIGHT-percentageY);
		case 4:
			return new Point(Constants.BOX_WIDTH-percentageX,Constants.BOX_HEIGHT-percentageY);
		default:
			return new Point(percentageX,percentageY);
		}	
		
	}
	
}
